package cn.newstrength.qiyiming.step;

import cn.newstrength.qiyiming.data.QYMSampleObj;
import cn.newstrength.qiyiming.data.QYMSampleObj;
import cn.newstrength.wtdf.plugin.service.JWTService;
import cn.newstrength.wtdf.plugin.service.RedisService;
import cn.newstrength.wtdf.plugin.util.TranUtils;
import cn.newstrength.wtdf.web.processor.Operation;
import cn.newstrength.wtdf.web.processor.OperationStep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class JWTStep implements OperationStep {
	
	private static final Logger logger = LoggerFactory.getLogger(JWTStep.class);
	
	@Override
	public int excute(Operation oper) {
		/*
		 * 以上为用户登录成功后的流程，此处不演示
		 */
		QYMSampleObj sampleObj = new QYMSampleObj();
		sampleObj.setId(666);
		sampleObj.setCode("Xd");
		sampleObj.setName("王新栋");
		logger.info("测试数据 {}",TranUtils.toJson(sampleObj));
		/*
		 * 生成Token
		 */
		String accessToken = JWTService.createJWT(sampleObj.getCode());
		logger.info("用户Token生成 {}",accessToken);
		/*
		 * 将token作为key，userObj作为value存储到Redis缓存中
		 */
		RedisService redisService = oper.getBean("redisService", RedisService.class);
		redisService.put("cn:newstrength:nsms:token:".concat(accessToken), sampleObj, 2*60*60);// 2小时
		logger.info("用户Token缓存生成！");
		/*
		 * 回传数据
		 */
		oper.setTranStepSuccResult(accessToken);
		return 0;
	}

	@Override
	public int init(Map<String, String> arg0) {
		return 0;
	}
}

